package cn.bone.ou.data.service.impl;

import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.mapper.HouseMapper;
import cn.bone.ou.data.dto.OpLoggerDto;
import cn.bone.ou.data.mapper.CommunityArticleMapper;
import cn.bone.ou.data.mapper.CommunityFileMapper;
import cn.bone.ou.data.mapper.CommunityNoticeMapper;
import cn.bone.ou.data.mapper.OpLoggerMapper;
import cn.bone.ou.data.param.OpLoggerParam;
import cn.bone.ou.data.service.IOpLoggerService;
import cn.bone.ou.data.vo.OpLoggerVo;
import cn.bone.ou.user.mapper.UserMapper;
import cn.bone.ou.vote.mapper.VoteInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OpLoggerServiceImpl implements IOpLoggerService {

    @Resource
    private OpLoggerMapper loggerMapper;
    @Resource
    private HouseMapper houseMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommunityFileMapper fileMapper;
    @Resource
    private CommunityArticleMapper articleMapper;
    @Resource
    private CommunityNoticeMapper noticeMapper;
    @Resource
    private VoteInfoMapper voteInfoMapper;

    public CommonResult list(OpLoggerParam.ListParam param){

        List<OpLoggerDto.ListDto> dtos = loggerMapper.list(
                param.getStart(), param.getNum(),param.getCommunityId());
        List<OpLoggerVo.ListVo> vos = dtos.stream().map(dto->{
            OpLoggerVo.ListVo vo = new OpLoggerVo.ListVo();
            vo.setLoggerId(dto.getId());
            vo.setMsg(dto.getMsg());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(dto.getCreateTime()));
            vo.setPhoneNum(dto.getPhoneNum());
            vo.setRealName(dto.getRealName());
            return vo;
        }).collect(Collectors.toList());
        int count = loggerMapper.countList(param.getCommunityId());
        DataGrid<OpLoggerVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    public CommonResult<OpLoggerVo.HomeVo> home(OpLoggerParam.HomeParam param){

        OpLoggerVo.HomeVo vo = new OpLoggerVo.HomeVo();

        //房屋
        int house = houseMapper.countAll(param.getCommunityId());
        vo.setHouse(house);

        //业主,住户
        int yz = userMapper.countAllYz(param.getCommunityId());
        int zh = userMapper.countAllZh(param.getCommunityId());
        vo.setYezhu(yz);
        vo.setZhuhu(zh);

        //文档
        int file = fileMapper.countAll(param.getCommunityId());
        int article = articleMapper.countAll(param.getCommunityId());
        int document = file + article;
        vo.setDocument(document);

        //公告
        int notice = noticeMapper.countAll(param.getCommunityId());
        vo.setNotice(notice);

        //报事报修
        vo.setBsbx(0);

        //投票议事
        int vote = voteInfoMapper.countAll(param.getCommunityId());
        vo.setVote(vote);

        return Ref.ok(vo);
    }
}
